using System;
using System.Net;
using Grundy.Common;
using FluentValidation;
using Grundy.Message;
using Grundy.Message.Administration;
using log4net;

namespace Grundy.Licensing.Server.Command
{
    public class ServerCommand : ICommand
    {
        ILog _logger = log4net.LogManager.GetLogger(typeof(ServerCommand));
        private static ServerCommandRequestValidator _validator = new ServerCommandRequestValidator();

        public bool CanProcess(Request request)
        {
            return request is ServerCommandRequest;
        }

        public Response Process(Request request, EndPoint requestingEndPoint, Guid customerId, string customerName)
        {
            return Process((ServerCommandRequest)request);

        }
        
        private Response Process(ServerCommandRequest request)
        {
            _validator.ValidateAndThrow(request);

            switch (request.Command)
            {
                case Message.Administration.ServerCommand.Start:
                    Application.Instance.LicenseServer.Start();
                    break;
                case Message.Administration.ServerCommand.Shutdown:
                    Application.Instance.LicenseServer.Shutdown();
                    break;
                case Message.Administration.ServerCommand.Restart:
                    Application.Instance.LicenseServer.Restart();
                    break;
            }
            return new Success{};
        }
    }
}